package My.project.Ui.Activity;

import My.project.Ui.R;
import My.project.Util.Connector;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This activity is used to fill in information of the uploading video.
 * It can check fields of the form.
 * After uploading, it will show a dialog to inform the result, and then
 * switch to MainActivity.
 * @author Bell
 *
 */
public class UploadActivity extends Activity {

	private Button upLoadButton;
	private Button cancelButton;
	private EditText et;
	private String filepath = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.upload);
		
		// Get the message from the intent
	    Intent intent = this.getIntent();
	    Bundle bundle = intent.getExtras();
	    filepath = bundle.getString("filepath");
		
		upLoadButton = (Button)findViewById(R.id.button_upload);
		upLoadButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				et = (EditText) findViewById(R.id.videoname);
				String videoName = et.getText().toString();
				
				et = (EditText) findViewById(R.id.videodes);
				String videoDes = et.getText().toString();
				
				System.out.println(videoName);
				if (videoName.equals("")) {
					Context context = getApplicationContext();
					CharSequence text = "Please fill in video name";
					int duration = Toast.LENGTH_SHORT;
					Toast.makeText(context, text, duration).show();
				} else {
					// connect to server
					Connector con = new Connector();
	        		String result = con.upload(filepath, videoName, videoDes);
	        		showDialog(result);
				}
			}
		});
		
		cancelButton = (Button) findViewById(R.id.button_cancel);
		cancelButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				showConfirmDialog();
			}
		});
	}
	
	//menu button action
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, R.string.str_about);
		menu.add(0, 1, 1, R.string.str_exit);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
		case 0:
			openOptionsDialog();
			break;
		case 1:
			exitOptionsDialog();
			break;
		default:
			break;
		}
		return true;
	}

	private void showDialog(String mess){
    	new AlertDialog.Builder(UploadActivity.this).setTitle("Message")
    	.setMessage(mess)
    	.setNegativeButton("confirm", new DialogInterface.OnClickListener(){
    		public void onClick(DialogInterface dialog, int which){
        		returnToMain();
    		}
    	}).show();
    }
	
	private void returnToMain() {
		Intent parentActivityIntent = new Intent(
        		this, MainActivity.class);
        parentActivityIntent.addFlags(
                Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(parentActivityIntent);
        finish();
	}
	
	private void showConfirmDialog() {
		new AlertDialog.Builder(this).setTitle(R.string.str_cancel)
		.setMessage(R.string.video_cancel_msg)
		.setNegativeButton(R.string.str_no,
				new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		})
		.setPositiveButton(R.string.str_yes,
				new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				returnToMain();
			}
		}).show();
	}
	
	// show author info
	private void openOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_about)
		.setMessage(R.string.app_about_msg)
		.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		}).show();
	}
	
	// show exit dialog
	private void exitOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_exit)
		.setMessage(R.string.app_exit_msg)
		.setNegativeButton(R.string.str_no, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		})
		.setPositiveButton(R.string.str_yes, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				Intent intent = new Intent(Intent.ACTION_MAIN);
  	            intent.addCategory(Intent.CATEGORY_HOME);
  	            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  	            startActivity(intent);
  	            android.os.Process.killProcess(android.os.Process.myPid());
			}
		}).show();
	}
}
